# coding:utf-8
from apps.cm.CmProductInfo import SmUsermessage
from tools import DBUtil
from tools.Encryption import Encryption
from tools.SingleTon import Singleton
from tools import DBUtil

class SmManager(Singleton):
    def login(self,loginname, loginpass):
        '''
        用户登录密码验证（手机号码、用户ID，根据数字校验是根据手机号码还是用户） , user替换一下
        :param loginname: String
        :param loginpass: String
        :return: SmUsermessage
        '''
        if (loginname == "" or loginpass == ""):
            raise Exception(u"用户或者密码为空")

        sql = "select userpassword from sm_usermessage where login_name='%s'" % loginname
        src_pwd = DBUtil.executeQuery_one(sql)
        if (src_pwd == ""):
            raise Exception(u"用户不存在")

        src_pwd = Encryption().EncryptionEngine(src_pwd[0], "", False) # 对数据库中的密码解密
        if (src_pwd != loginpass):
            raise Exception(u"密码错误")

        # 获取用户信息，存储到session
        user = SmUsermessage(login_name=loginname)


        sql = "select * from sm_usermessage where login_name='" + loginname + "'";
        res = DBUtil.executeQueryMap(sql)
        user.user_id = res[0].get('user_id')
        user.user_name = res[0].get('user_name')
        user.login_name = res[0].get('login_name')
        user.user_id = res[0].get('user_id')

        return user
        




def getOrderUrl():
    sql = "select item_value from cm_dict where dict_type='server.config' and item_cn='ui.orderUrl'"
    orderurl = DBUtil.executeQuery_one(sql)
    return  orderurl[0]


def getWsUrl():
    sql = "select item_value from cm_dict where dict_type='server.config' and item_cn='ui.wsUrl'"
    wsurl = DBUtil.executeQuery_one(sql)
    return  wsurl[0]

def getLoginUserID(request):
    loginUser = request.session.get("user")
    print '---login user ----'
    print loginUser
    return loginUser

